package Teacher.Course;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.util.ArrayList;

import javax.swing.JScrollPane;
import javax.swing.JTable;

import Database.CallDB;
import Database.CourseMgmt;
import Database.MyTableModel;
import Exception.CheckDataException;
import Exception.EraseDataException;
import Exception.FormatScrollPaneException;
import Exception.InsertionDataException;
import Exception.Layer8DataException;
import Exception.LoadDriverException;
import General.CreaModPanel;
import General.GeneralInformation;
import General.GeneralMethod;
import General.GraphicsSetting;
import General.TreatError;
import GenericClass.FormatScrollPane;

public class PanelCreateModCourse extends CreaModPanel
{
	private ArrayList<Integer> arrayIdClass = new ArrayList<Integer>();
	private ArrayList<Integer> arrayIdAccount = new ArrayList<Integer>();
	private MyTableModel myAccountModel;
	private JTable tableAccount;
	private JScrollPane scrollAccount;
	private FormatScrollPane formatScrollPane;
	private String buttonTitle;
	private boolean modifyFlag = false;
	
	public PanelCreateModCourse(CallDB callDB, Connection connection) throws CheckDataException, FormatScrollPaneException
	{
		super(callDB, connection);

		modifyFlag = false;
		
		myRightTableModel = GeneralMethod.getRightModel();
		
		refreshSuperRightTable();
		creationComponent();
		showComponent();
	}
	
	public PanelCreateModCourse(CallDB callDB, Connection connection, String fieldName) throws CheckDataException, FormatScrollPaneException
	{
		super(callDB, connection);

		modifyFlag = true;
		this.fieldName.setText(fieldName);
		
		myRightTableModel = GeneralMethod.getRightModel();
		refreshSuperRightTable();
		creationComponent();
		showComponent();
	}
	
	public void setCourse() throws InsertionDataException, CheckDataException, EraseDataException, LoadDriverException
	{
		convertAndGetIndexValue();

		if(modifyFlag == false)
			new CourseMgmt(connection).createElement(fieldName.getText(), arrayIdClass, arrayIdAccount);
		else
			new CourseMgmt(connection).modifyElement(GeneralInformation.getIdSelectedLeftTable(), fieldName.getText(), arrayIdClass, arrayIdAccount);
	}
	
	public void convertAndGetIndexValue()
	{
		ArrayList<Integer> arrayConvertRight = new ArrayList<Integer>(); //Temporary variable
		ArrayList<Integer> arrayConvertAccount = new ArrayList<Integer>(); //Temporary variable
		
		for(int index : tableRight.getSelectedRows()){ //For each rows put it in index
			arrayConvertRight.add(tableRight.convertRowIndexToModel(index));} //Convert table index in model index
		
		for(int index : tableAccount.getSelectedRows()){ //For each rows put it in index
			arrayConvertAccount.add(tableAccount.convertRowIndexToModel(index));} //Convert table index in model index
		
		for(int value : arrayConvertRight) //For each index
			arrayIdClass.add((int)myRightTableModel.getValueAt(value, 3)); //Get the id
		
		for(int value : arrayConvertAccount) //For each index
			arrayIdAccount.add((int)myAccountModel.getValueAt(value, 2)); //Get the id
	}

	private class MyActionListener implements ActionListener
	{
		public void actionPerformed(ActionEvent arg0)
		{
			try
			{
				if(arg0.getSource() == buttonDone)
				{
					if(fieldName.getText().equals("") != true)
					{
						if(modifyFlag == false)
						{
							boolean differentFieldName = new CourseMgmt(connection).checkFieldName(fieldName.getText());
	
							if(differentFieldName == true)
								setCourse();
							else
								throw new Layer8DataException("This name is already used !");
						}
						else
							setCourse();
					}
					else
						throw new Layer8DataException();
					
					clearArray();
				}
			}
			catch(CheckDataException | InsertionDataException | EraseDataException | LoadDriverException | Layer8DataException error)
			{
				TreatError.showError(error);
			}
		}
	}
	
	public void clearArray()
	{
		arrayIdAccount.clear();
		arrayIdClass.clear();
	}
	
	public void creationComponent() throws CheckDataException
	{	
		MyActionListener actionListener = new MyActionListener();
		
		tableAccount = new JTable();
		formatScrollPane = new FormatScrollPane();
		scrollAccount = new JScrollPane();
		
		myAccountModel = GeneralInformation.getMyAccountModel();
		buttonDone.addActionListener(actionListener);
	}
	
	public void showAccountTable() throws FormatScrollPaneException
	{
		formatScrollPane.autoFillScrollPane(tableAccount, myAccountModel, GeneralMethod.getArrayAccountHeader(), scrollAccount, "multiple", true, true, true, false);
		validate();
	}
	
	public void showComponent() throws FormatScrollPaneException
	{
		scrollAccount.setBounds(39, 111, 251, 249);
		this.add(scrollAccount);
		
		showAccountTable();
		
		GraphicsSetting.setBackground(this, "image/main.png", 0, 0, 800, 600);
	}
}